package com.gears_software.sxdc.dccx.data.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.data.model.CPOrderModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.ui.activity.MainActivity;
import com.gears_software.sxdc.dccx.ui.activity.ReleaseScheduleActivity_;
import com.gears_software.sxdc.dccx.ui.event.UpdateDriverVehicleFullEvent;
import com.gears_software.sxdc.dccx.ui.fragment.CarOwnerFragment;
import com.gears_software.sxdc.dccx.ui.fragment.CarOwnerFragment_;
import com.gears_software.sxdc.dccx.ui.util.OttoBus;
import com.gears_software.sxdc.dccx.ui.util.OttoBus_;
import com.gears_software.sxdc.dccx.ui.view.CheZhuXingCheListItemView;
import com.gears_software.sxdc.dccx.ui.view.CheZhuXingCheListItemView_;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.androidannotations.annotations.Background;
import org.springframework.http.HttpOutputMessage;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * Created by chezhenzhen on 2017/6/27.
 */

public class CheZhuXingChengListAdapter extends RecyclerView.Adapter<CheZhuXingChengListAdapter.ViewHolder> implements View.OnClickListener {
    Context context;
    List<CPOrderModel> list;
    Fragment fragment;
    private OnItemClickListener mOnItemClickListener = null;

    //define interface
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public CheZhuXingChengListAdapter(Fragment fragment, Context context, List<CPOrderModel> list) {
        super();
        this.fragment = fragment;
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_view_chezhu_xingcheng, viewGroup, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        if (payloads == null || payloads.size() < 1) {
            onBindViewHolder(holder, position);
        } else {
            CPOrderModel model = list.get(position);
            ViewHolder viewHolder = holder;
            viewHolder.txt_set.setText("剩余" + model.getPassengernum() + "座");
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        holder.txt_triptime_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_jiantou_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_cheman_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_xiugai_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        holder.txt_man_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        CPOrderModel orderModel = list.get(position);
        if (orderModel != null) {
            holder.txt_vehicleno.setText(orderModel.getVehicleno());
            holder.txt_car.setText(orderModel.getCarmodel());
            holder.txt_vehiclecolor.setText(orderModel.getVehiclecolor());
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            holder.txt_date.setText(sf.format(orderModel.getDeparttime()));
            holder.txt_start.setText(orderModel.getStartpoint());
            holder.txt_end.setText(orderModel.getEndpoint());
            holder.txt_set.setText("剩余 " + orderModel.getPassengernum() + " 座");
            holder.txt_money.setText(String.valueOf(orderModel.getPrice()));
            if (orderModel.getOrderstatus() != 0) {
                holder.ly_xiugai.setVisibility(View.GONE);
            } else {
                holder.ly_xiugai.setVisibility(View.VISIBLE);
            }
            if (orderModel.getPassengernum() == 0) {
                holder.txt_man_icon.setVisibility(View.VISIBLE);
                holder.re_driver_botttom.setVisibility(View.GONE);
            } else {
                holder.txt_man_icon.setVisibility(View.GONE);
                holder.re_driver_botttom.setVisibility(View.VISIBLE);
            }
            if (orderModel.getCanceltime() != null) {
                holder.txt_man_icon.setVisibility(View.VISIBLE);
                holder.txt_man_icon.setText(R.string.carowner_trip_item_cancel_icon);
            }
        }
        holder.ly_xiugai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ReleaseScheduleActivity_.class);
                intent.putExtra("orderModel", list.get(position));
                intent.putExtra("title", "修改行程");
                intent.putExtra("flag", "update");
                intent.putExtra("position", position);
                fragment.startActivity(intent);
            }
        });
        holder.ly_cheman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(context)
                        .title("提示")
                        .titleGravity(GravityEnum.CENTER)
                        .titleColorRes(R.color.colorFontGray)
                        .content("您确定将此行程设为车满吗?")
                        .contentColorRes(R.color.black)
                        .positiveText("确定")
                        .positiveColor(ContextCompat.getColor(context, R.color.colorFontBlue))
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                OttoBus_.getInstance_(context).post(new UpdateDriverVehicleFullEvent(position));

                            }
                        })
                        .negativeText("取消")
                        .negativeColor(ContextCompat.getColor(context, R.color.colorFontGray))
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                            }
                        })
                        .buttonsGravity(GravityEnum.CENTER)
                        .show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txt_triptime_icon;
        public TextView txt_jiantou_icon;
        public TextView txt_cheman_icon;
        public TextView txt_xiugai_icon;
        public TextView txt_car;
        public TextView txt_vehiclecolor;
        public TextView txt_vehicleno;
        public TextView txt_date;
        public TextView txt_start;
        public TextView txt_end;
        public TextView txt_set;
        public TextView txt_money;
        public LinearLayout ly_xiugai;
        public LinearLayout ly_cheman;
        public RelativeLayout re_driver_botttom;
        public TextView txt_man_icon;


        public ViewHolder(View view) {
            super(view);
            txt_triptime_icon = (TextView) view.findViewById(R.id.txt_triptime_icon);
            txt_jiantou_icon = (TextView) view.findViewById(R.id.txt_jiantou_icon);
            txt_cheman_icon = (TextView) view.findViewById(R.id.txt_cheman_icon);
            txt_car = (TextView) view.findViewById(R.id.txt_car);
            txt_vehiclecolor = (TextView) view.findViewById(R.id.txt_vehiclecolor);
            txt_vehicleno = (TextView) view.findViewById(R.id.txt_vehicleno);
            txt_xiugai_icon = (TextView) view.findViewById(R.id.txt_xiugai_icon);
            txt_date = (TextView) view.findViewById(R.id.txt_date);
            txt_start = (TextView) view.findViewById(R.id.txt_start);
            txt_end = (TextView) view.findViewById(R.id.txt_end);
            txt_set = (TextView) view.findViewById(R.id.txt_set);
            txt_money = (TextView) view.findViewById(R.id.txt_money);
            ly_xiugai = (LinearLayout) view.findViewById(R.id.ly_xiugai);
            ly_cheman = (LinearLayout) view.findViewById(R.id.ly_cheman);
            re_driver_botttom = (RelativeLayout) view.findViewById(R.id.re_driver_botttom);
            txt_man_icon = (TextView) view.findViewById(R.id.txt_man_icon);
        }
    }

}
